package cn.tedu.sp04.service;

import cn.tedu.entity.Item;
import cn.tedu.entity.Order;
import cn.tedu.service.OrderService;
import cn.tedu.sp04.feigh.ItemClient;
import cn.tedu.sp04.feigh.UserClient;
import cn.tedu.web.util.JsonResult;
import com.netflix.discovery.converters.Auto;
import com.sun.org.apache.xpath.internal.operations.Or;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class OrderServiceImpl implements OrderService {
    @Autowired
    private ItemClient itemClient;
    @Autowired
    private UserClient userClient;
    @Override//获取订单
    public Order getOrder(String orderId) {
        //需求是：获取商品列表
        //通过注入的itemClient接口（这个接口调用了itemService【商品】）
        JsonResult<List<Item>> item=itemClient.getItems(orderId);

        Order order=new Order();
        order.setId(orderId);
        return order;
    }

    @Override//添加订单的逻辑
    public void addOrder(Order order) {
        //TODO: 调用item-service减少商品库存
        //TODO: 调用user-service增加用户积分
        log.info("保存订单："+order);
        //远程调用商品，减少库存
        itemClient.decreaseNumber(order.getItems());
        //远程调用用户，增加积分
        userClient.addScore(order.getUser().getId(),1000);

    }
}
